package action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.MissingResourceException;

import javax.sql.DataSource;

import login.dao.MyDataSource;

/**
 * @author Kiran Jakkaraju
 * 
 */
public class Login {

	String sqlurl = null;

	public void ProcessLogin() {
		
		this.setDbusername("");
		this.setDbpassword("");


		Connection conn = null;
		Statement stmt = null;
		ResultSet rset = null;

		try {

			conn = this.connect().getConnection();
			stmt = conn.createStatement();

			rset = stmt.executeQuery(this.getSqlurl() + " where username='"
					+ this.getUsername() + "' and password='"
					+ this.getPassword() + "'");

			while (rset.next()) {

				this.setDbusername(rset.getString(1));
				this.setDbpassword(rset.getString(2));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (MissingResourceException mre) {
			mre.printStackTrace();
		} finally {
			try {
				rset.close();
			} catch (Exception e) {
			}
			try {
				stmt.close();
			} catch (Exception e) {
			}
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
	}

	public DataSource connect() {

		MyDataSource myds = new MyDataSource();
		DataSource dataSource = myds.setupDataSource();

		return dataSource;

	}

	public boolean checkUserCredentials() {

		if (this.getDbusername().equalsIgnoreCase(this.getUsername())
				&& this.getDbpassword().equals(this.getPassword()))
			return true;
		else
			return false;

	}

	String username = null;
	String password = null;

	String dbusername = null;
	String dbpassword = null;

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the sqlurl
	 */
	public String getSqlurl() {
		return sqlurl;
	}

	/**
	 * @param sqlurl
	 *            the sqlurl to set
	 */
	public void setSqlurl(String sqlurl) {
		this.sqlurl = sqlurl;
	}

	/**
	 * @return the dbusername
	 */
	public String getDbusername() {
		return dbusername;
	}

	/**
	 * @param dbusername
	 *            the dbusername to set
	 */
	public void setDbusername(String dbusername) {
		this.dbusername = dbusername;
	}

	/**
	 * @return the dbpassword
	 */
	public String getDbpassword() {
		return dbpassword;
	}

	/**
	 * @param dbpassword
	 *            the dbpassword to set
	 */
	public void setDbpassword(String dbpassword) {
		this.dbpassword = dbpassword;
	}

}
